package com.jr.service.impl;

import com.jr.mapper.OrderMapper;
import com.jr.pojo.Order;
import com.jr.service.OrderService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;

@Service
public class OrderServiceImpl implements OrderService {

    @Autowired
    private OrderMapper orderMapper;

    @Override
    public boolean createOrder(Order order) {
        return orderMapper.insertOrder(order) > 0;
    }

    @Override
    public ArrayList<Order> getOrdersByUserId(Integer userId, String userType) {
        return orderMapper.selectByUserId(userId, userType);
    }

    @Override
    public Order getOrderById(Integer orderId) {
        return orderMapper.selectById(orderId);
    }

    @Override
    public boolean updateOrderStatus(Integer orderId, String status) {
        return orderMapper.updateStatus(orderId, status) > 0;
    }

    @Override
    public ArrayList<Order> getOrdersByPage(Integer userId, String userType, Integer pageIndex, Integer pageSize) {
        int offset = (pageIndex - 1) * pageSize;
        return orderMapper.selectByPage(userId, userType, offset, pageSize);
    }

    @Override
    public ArrayList<Order> getOrdersByPage(Integer userId, String userType, Integer pageIndex, Integer pageSize, String status) {
        int offset = (pageIndex - 1) * pageSize;
        return orderMapper.selectByPageWithStatus(userId, userType, offset, pageSize, status);
    }

    @Override
    public int getOrderCount(Integer userId, String userType) {
        return orderMapper.countByUserId(userId, userType);
    }

    @Override
    public int getOrderCount(Integer userId, String userType, String status) {
        return orderMapper.countByUserIdWithStatus(userId, userType, status);
    }
}
